package com.study.mike.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.study.mike.mapper.SysOperLogMapper;
import com.study.mike.model.dto.PageDTO;
import com.study.mike.model.dto.SysLogDTO;
import com.study.mike.model.entity.SysOperLogDO;
import com.study.mike.model.po.SysLogPO;
import com.study.mike.service.SysOperLogService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author KangHui
 * @Date 2022/9/26 14:05
 */
@Service
public class SysOperLogServiceImpl extends ServiceImpl<SysOperLogMapper, SysOperLogDO> implements SysOperLogService {
    @Override
    public PageDTO<SysLogDTO> query(SysLogPO po) {
        LambdaQueryWrapper<SysOperLogDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StrUtil.isNotBlank(po.getTitle()), SysOperLogDO::getTitle, po.getTitle())
                .eq(ObjectUtil.isNotNull(po.getMethodType()), SysOperLogDO::getMethodType, po.getMethodType())
                .like(StrUtil.isNotBlank(po.getMethodName()), SysOperLogDO::getMethodName, po.getMethodName())
                .eq(ObjectUtil.isNotNull(po.getStatus()), SysOperLogDO::getStatus, po.getStatus())
                .like(StrUtil.isNotBlank(po.getOperation()), SysOperLogDO::getOperation, po.getOperation())
                .between(ObjectUtil.isAllNotEmpty(po.getStartTime(), po.getEndTime()), SysOperLogDO::getOperationTime,
                        po.getStartTime(), po.getEndTime())
                .orderByDesc(SysOperLogDO::getId);
        IPage<SysOperLogDO> page = this.page(new Page<>(po.getPageCurrent(), po.getPageSize()), wrapper);
        List<SysOperLogDO> records = page.getRecords();
        List<SysOperLogDO> collect = JSONUtil.toList(JSONUtil.toJsonStr(records), SysOperLogDO.class);
        PageDTO<SysLogDTO> resultPage = new PageDTO<>(page.getCurrent(), page.getSize(), page.getTotal());
        resultPage.setRecords(collect);
        return resultPage;
    }

    @Override
    public Boolean edit(SysLogPO po) {
        SysOperLogDO sysOperLogDO = new SysOperLogDO();
        BeanUtils.copyProperties(po, sysOperLogDO);
        return this.updateById(sysOperLogDO);
    }

    @Override
    public Boolean deleteBatch(List<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return false;
        }
        return this.removeByIds(ids);
    }


}
